package com.leonzx.base.config.beetl.tag;


import com.leonzx.base.entity.system.Menu;
import org.beetl.core.GeneralVarTagBinding;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @Author:梁亮
 * @Description:
 * @Date: 2018/7/2 10:52
 */

/**
 * <li>
 * <a href="javascript:;">
 * <i class="iconfont">&#xe6b8;</i>
 * <cite>用户权限</cite>
 * <i class="iconfont nav_right">&#xe697;</i>
 * </a>
 * <ul class="sub-menu">
 * <li>
 * <a _href="${ctxPath}/userpermission/user/list">
 * <i class="iconfont">&#xe6a7;</i>
 * <cite>用户管理</cite>
 * </a>
 * </li>
 * <li>
 * <a _href="${ctxPath}/role/torolelist">
 * <i class="iconfont">&#xe6a7;</i>
 * <cite>角色管理</cite>
 * </a>
 * </li>
 * <li>
 * <a _href="${ctxPath}/menu/tomenu">
 * <i class="iconfont">&#xe6a7;</i>
 * <cite>菜单管理</cite>
 * </a>
 * </li>
 * </ul>
 * </li>
 */
public class MenuTag extends GeneralVarTagBinding {
    private String urlRoot;

    @Override
    public void render() {
        List<Menu> menus = (List<Menu>) getAttributeValue("val");
        String top = (String) getAttributeValue("top");
        urlRoot = (String) getAttributeValue("ctx");
        try {
            String html = "";
            if (menus != null) {

                if (Objects.equals(top, "1")) {
                    html = getTopMenuHtml(menus);
                } else {
                    html = getMenuHtml(menus);
                }
            }
            ctx.byteWriter.writeString(html);
            this.doBodyRender();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private String getMenuHtml(List<Menu> menus) {
        String html = "";

        for (Menu menu : menus) {
            //判断当前菜单是否有子菜单
            //无子菜单
            if (menu.getSonMenus() == null || menu.getSonMenus().isEmpty()) {
                html += " <li>\n" +
                        "<a _href=\"" + urlRoot + menu.getUrl() + "\">\n" +
                        "<i class=\"layui-icon " + menu.getIcon() + "\"></i>\n" +
                        "<cite>" + menu.getName() + "</cite>\n" +
                        "</a>\n" +
                        "</li>";
                //有子菜单
            } else {
                html += "<li>\n" +
                        "<a href=\"javascript:;\">\n" +
                        "<i class=\"layui-icon " + menu.getIcon() + "\"></i>\n" +
                        "<cite>" + menu.getName() + "</cite>\n" +
                        "<i class=\"iconfont nav_right\">&#xe697;</i>\n" +
                        "</a>\n" +
                        "<ul class=\"sub-menu\">\n" +
                        getMenuHtml(menu.getSonMenus()) +
                        "</ul>\n" +
                        "</li>";
            }
        }
        return html;
    }

    /**
     * <li class="layui-nav-item">
     * <a href="javascript:;">用户权限</a>
     * <dl class="layui-nav-child">
     * <dd>
     * <a href=""><i class="iconfont">&#xe6b8;</i>用户信息</a>
     * <ul style='margin:0px 0px 0px 45px;'>
     * <li>
     * <a _href="${ctxPath}/userpermission/user/list">
     * <i class="iconfont">&#xe6a8;</i>
     * <cite>用户管理</cite>
     * </a>
     * </li>
     * </ul>
     * </dd>
     * <dd><a onclick="setUrl('${ctxPath}/role/torolelist')"><i class="iconfont">&#xe6b8;</i>角色管理</a></dd>
     * <dd><a onclick="setUrl('${ctxPath}/menu/tomenu')"><i class="iconfont">&#xe6b8;</i>菜单管理</a></dd>
     * </dl>
     * </li>
     *
     * <#menu val="${menus}" ctx="${ctxPath}" top="1"></#menu>
     * @param menus
     * @return
     */
    private String getTopMenuHtml(List<Menu> menus) {
        String html = "";
        for (Menu menu : menus) {
            //判断当前菜单是否有子菜单
            //无子菜单


            if (menu.getSonMenus() == null || menu.getSonMenus().isEmpty()) {
                html += "<li class=\"layui-nav-item\">\n" +
                        "<a _href=\"" + menu.getUrl() + "\"> \n" +
                        "<i class=\"layui-icon " + menu.getIcon() + "\"></i>\n" +
                        "<cite>" + menu.getName() + "</cite>\n" +
                        "</a>";
                //有子菜单
            } else {
                html += "<li class=\"layui-nav-item\">\n" +
                        "<a href=\"javascript:void(0);\"> \n" +
                        "<i class=\"layui-icon " + menu.getIcon() + "\"></i>\n" +
                        "<cite>" + menu.getName() + "</cite>\n" +
                        "</a>";

                List<Menu> subMenus = menu.getSonMenus();
                html += "<dl class=\"layui-nav-child\">\n";
                for (Menu subMenu : subMenus) {
                    if (subMenu.getSonMenus() == null || subMenu.getSonMenus().isEmpty()) {
                        html +=
                                "<dd>\n" +
                                "<a _href=\"" + subMenu.getUrl() + "\">" +
                                "<i class=\"layui-icon " + subMenu.getIcon() + "\"></i>\n" +
                                "<cite>" + subMenu.getName() + "</cite>\n" +
                                "</a>\n" +
                                "</dd>\n";

                    } else {
                        html += "<dd>\n" +
                                "<a href=\"javacript:void(0);\">" +
                                "<i class=\"layui-icon " + subMenu.getIcon() + "\"></i>\n" +
                                "<cite>" + subMenu.getName() + "</cite>\n" +
                                "</a>\n" +
                                "<ul style='margin:0px 0px 0px 45px;'>\n";
                        for (Menu ssub : subMenu.getSonMenus()) {
                            html += "<li>\n" +
                                    "<a _href=\"" + ssub.getUrl() + "\">\n" +
                                    "<i class=\"layui-icon " + ssub.getIcon() + "\"></i>\n" +
                                    "<cite>" + ssub.getName() + "</cite>\n" +
                                    "</a>\n" +
                                    "</li>\n";
                        }
                        html += "</ul></dd>";
                    }
                }
                html += "</dl>";
            }
            html += "</li>";
        }
        return html;
    }
}